<?php

require_once '../lib/pdowrapper.php';
require_once '../lib/password.php';

$email = $_POST['email'];
$password = trim($_POST['password']);

$validEmail = DB::run('SELECT * FROM employees WHERE email=?', [$email])->fetch();

if($validEmail)
{
	$validPassword = password_verify($password, $validEmail['password']);

	if($validPassword)
	{
		$_SESSION['firstname'] = $validEmail['firstname'];
		$_SESSION['lastname'] = $validEmail['lastname'];
		$_SESSION['email'] = $email;

		echo 'Success! Logged in as '.$_SESSION['email'];
	}
	else
	{
		$_SESSION['success'] = false;

		echo 'Wrong password. Please try again.';
	}
}
else
{
	$_SESSION['success'] = false;

	echo 'Wrong email. Please try again.';
}

?>
